<?php

include("pac-mu.php");

function F_CRC2($data, $CRCi) {

	$output = "";

	$j = 0;
	
	for($nbyte=1;$nbyte<=sizeof($data);$nbyte++) {
		$C=(($output>>8)^$data[$j])<<8;
		for($i=0;$i<8;$i++) {
			if($C&0x8000) {
				$C = pack("S", $C<<1^0x1021);
				$C = unpack("S", $C);
				$C = $C[1];
			} else { 
					$C = pack("S", $C<<1);
					$C = unpack("S", $C);
					$C = $C[1];
				}
		}
		$output = pack("S", $C^($output<<8));
		$output = unpack("S", $output);
		$output = $output[1];
		$j++;
	}
	if($CRCi == "CRCH") {
		if(strlen(dechex($output)) == 3) {
			$output = "0".dechex($output);
			return substr($output, 0 , 2);
		} else {
			return substr(dechex($output), 0 , 2);
			}
	} elseif($CRCi == "CRCL") {
		if(strlen(dechex($output)) == 3) {
			return substr(dechex($output), 1 , 2);
		} else {
			return substr(dechex($output), 2);
		}
	}
	
}

function F_Frame_Length($data) {
	$frame_length = sizeof($data) + 5;
	return $frame_length;
}

function F_Frame_Constructor($c_frame) {
	$output = "";
	for($i=0;$i<sizeof($c_frame);$i++) {
		$hex = dechex($c_frame[$i]);
		if(strlen($hex) == 1) {
			$hex = "0".$hex;
			$frame[] = $hex;
		} else {
			$frame[] = $hex;
		}
		$output .= hex2bin($frame[$i]);
	}
	return $output;
}

function F_Get_Frame($g_frame) {
	for ($i=0; $i < sizeof($g_frame); $i++) {
		if(strlen($g_frame[$i]) == 1) {
			$g_frame[$i] = "0".$g_frame[$i];
		}
		$output[] = $g_frame[$i];
	}
	return $output;
}

function C_Reset() {
	
	$reset_frame = [ C_PAC_MU, 0x05, C_Reset ];
	
	$frame = F_Frame_Constructor($reset_frame);

	$CRCH = hex2bin(F_CRC2($reset_frame, "CRCH"));
	$CRCL = hex2bin(F_CRC2($reset_frame, "CRCL"));
	
	return $frame.$CRCH.$CRCL;

}

function C_Login_User($pass) { 
	
	$login_frame = [ C_PAC_MU, F_Frame_Length($pass), C_LoginUser ];
	
	for ($i=0; $i < sizeof($pass); $i++) {
		array_push($login_frame, F_Get_Frame($pass)[$i]);
	}
	
	$CRCH = F_CRC2($login_frame, "CRCH");
	$CRCL = F_CRC2($login_frame, "CRCL");
	
	array_push($login_frame, hexdec($CRCH));
	array_push($login_frame, hexdec($CRCL));
	
	$frame = F_Frame_Constructor($login_frame);
	
	return $frame;
}

function C_LogoutUser() {
	
	$logout_frame = [ C_PAC_MU, 0x05, C_LogoutUser ];
	
	$frame = F_Frame_Constructor($logout_frame);

	$CRCH = hex2bin(F_CRC2($logout_frame, "CRCH"));
	$CRCL = hex2bin(F_CRC2($logout_frame, "CRCL"));
	
	return $frame.$CRCH.$CRCL;
}

function C_SetAutoReaderConfig($config) {

	$config_frame = [ C_PAC_MU, F_Frame_Length($config), C_SetAutoReaderConfig ];
	
	for ($i=0; $i < sizeof($config); $i++) {
		array_push($config_frame, F_Get_Frame($config)[$i]);
	}
	
	$CRCH = F_CRC2($config_frame, "CRCH");
	$CRCL = F_CRC2($config_frame, "CRCL");
	
	array_push($config_frame, hexdec($CRCH));
	array_push($config_frame, hexdec($CRCL));
	
	$frame = F_Frame_Constructor($config_frame);
	
	return $frame;
}

function C_LoadKeyToSKB($key) {
	
	$key_frame = [ C_PAC_MU, F_Frame_Length($key), C_LoadKeyToSKB ];
	
	for ($i=0; $i < sizeof($key); $i++) {
		array_push($key_frame, F_Get_Frame($key)[$i]);
	}
	
	$CRCH = F_CRC2($key_frame, "CRCH");
	$CRCL = F_CRC2($key_frame, "CRCL");
	
	array_push($key_frame, hexdec($CRCH));
	array_push($key_frame, hexdec($CRCL));
	
	$frame = F_Frame_Constructor($key_frame);
	
	return $frame;
}

function C_TurnOnAntennaPower($mode) {
	
	$ant_frame = [ C_PAC_MU, F_Frame_Length($mode), C_TurnOnAntennaPower ];
	
	for ($i=0; $i < sizeof($mode); $i++) {
		array_push($ant_frame, F_Get_Frame($mode)[$i]);
	}
	
	$CRCH = F_CRC2($ant_frame, "CRCH");
	$CRCL = F_CRC2($ant_frame, "CRCL");
	
	array_push($ant_frame, hexdec($CRCH));
	array_push($ant_frame, hexdec($CRCL));
	
	$frame = F_Frame_Constructor($ant_frame);
	
	return $frame;
}

function C_Select($mode) {
	
	$select_frame = [ C_PAC_MU, F_Frame_Length($mode), C_Select ];
	
	for ($i=0; $i < sizeof($mode); $i++) {
		array_push($select_frame, F_Get_Frame($mode)[$i]);
	}
	
	$CRCH = F_CRC2($select_frame, "CRCH");
	$CRCL = F_CRC2($select_frame, "CRCL");
	
	array_push($select_frame, hexdec($CRCH));
	array_push($select_frame, hexdec($CRCL));
	
	$frame = F_Frame_Constructor($select_frame);
	
	return $frame;
}

function C_LoginWithSKB($data) {
	
	$skb_frame = [ C_PAC_MU, F_Frame_Length($data), C_LoginWithSKB ];
	
	for ($i=0; $i < sizeof($data); $i++) {
		array_push($skb_frame, F_Get_Frame($data)[$i]);
	}
	
	$CRCH = F_CRC2($skb_frame, "CRCH");
	$CRCL = F_CRC2($skb_frame, "CRCL");
	
	array_push($skb_frame, hexdec($CRCH));
	array_push($skb_frame, hexdec($CRCL));
	
	$frame = F_Frame_Constructor($skb_frame);
	
	return $frame;
}

function C_ReadBlock($data) {

	$skb_frame = [ C_PAC_MU, F_Frame_Length($data), C_ReadBlock ];
	
	for ($i=0; $i < sizeof($data); $i++) {
		array_push($skb_frame, F_Get_Frame($data)[$i]);
	}
	
	$CRCH = F_CRC2($skb_frame, "CRCH");
	$CRCL = F_CRC2($skb_frame, "CRCL");
	
	array_push($skb_frame, hexdec($CRCH));
	array_push($skb_frame, hexdec($CRCL));
	
	$frame = F_Frame_Constructor($skb_frame);
	
	return $frame;

}

function C_WriteBlock($data) {

	$skb_frame = [ C_PAC_MU, F_Frame_Length($data), C_WriteBlock ];
	
	for ($i=0; $i < sizeof($data); $i++) {
		array_push($skb_frame, F_Get_Frame($data)[$i]);
	}
	//echo var_dump($data);
	
	$CRCH = F_CRC2($skb_frame, "CRCH");
	$CRCL = F_CRC2($skb_frame, "CRCL");
	
	array_push($skb_frame, hexdec($CRCH));
	array_push($skb_frame, hexdec($CRCL));
	
	$frame = F_Frame_Constructor($skb_frame);
	
	return $frame;
}

?>